package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Supplier;
import com.atguigu.jxc.service.SupplierService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author zhmwi
 * @Date 2021/9/4
 * @Dersciption 供应商控制器
 */
@RestController
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    private SupplierService supplierService;
    /**
     * 分页查询供应商
     * @param page
     * @param rows
     * @param supplierName
     * @return
     */

    @PostMapping("/list")
    public Map<String,Object> getSupplierList(Integer page,Integer rows ,String supplierName){
        Map result = new HashMap<>();
        PageInfo<Supplier> pageInfo = supplierService.getSupplierList(page,rows,supplierName);
        long total = pageInfo.getTotal();
        List<Supplier> supplierList = pageInfo.getList();
        result.put("total",total);
        result.put("rows",supplierList);

        return result;
    }

    /**
     * 更新供应商 save?supplierId=1
     * @param supplier
     * @return
     */
    @PostMapping("save")
    public ServiceVO saveSupplier(Supplier supplier){
        //获取路径中的供应商Id
        Integer supplierId = supplier.getSupplierId();
        if (supplierId == null){
            //新增
            boolean isSave = supplierService.save(supplier);
            if (isSave){
                return new ServiceVO(100,"请求成功");
            }
        }else{
            //更新
            Boolean isUpdate = supplierService.update(supplier);
            if (isUpdate){
                return new ServiceVO(100,"请求成功");
            }
        }
        return new ServiceVO(300,"请求失败");

    }

    /**
     * 批量删除供应商
     * @param ids 1,2,3
     * @return
     */
    @PostMapping("delete")
    public ServiceVO deleteSupplier(String ids){
        boolean isDelete = supplierService.delete(ids);
        if (isDelete){
            return new ServiceVO(100,"请求成功");
        }else {
            return new ServiceVO(300,"请求失败");
        }
    }

    /**
     * 供应商名称模糊查询
     * @param q
     * @return
     */
    @PostMapping("getComboboxList")
    public List<Supplier> getComboboxList(String q){
        return supplierService.getComboboxList(q);
    }
}
